#include "fpga_axi_lite.h"
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

CFpga_axi_lite::CFpga_axi_lite()
{

}
CFpga_axi_lite::~CFpga_axi_lite()
{

}

int CFpga_axi_lite::init(int base_addr, int len)
{
    if (m_mem_fd != -1)
    {
        m_fd = m_mem_fd;
    }
    else
    {
        m_fd = ::open("/dev/mem", O_RDWR | O_SYNC);
        if (m_fd != 0)
        {
            //printf("Success to open /dev/mem fd = %08x\n", fd);
        }
        else
        {
            printf("openHJLib 1 Fail: %d %s\n", errno, strerror(errno));
            return -1;
        }
    }

    if (len == 0)
    {
        m_data_len = 4;
    }
    else
    {
        m_data_len = len;
    }
    m_map_base = mmap(nullptr, m_data_len, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, base_addr);

    if (m_map_base == 0)
    {
        printf("open mmap 2 Fail: %d %s\n", errno, strerror(errno));
        close(m_fd);
        return -1;
    }

    printf("CFpga_axi_lite mmap Video Cpature Pipeline to %d read_mmap_len %d ",
        m_map_base, m_data_len);
  
    m_base_addr = base_addr;
	
	return 0;
}

void CFpga_axi_lite::uninit()
{

}

int CFpga_axi_lite::freeze()
{
    *(volatile int* )m_map_base = 0x02;
    return 0;
}

int CFpga_axi_lite::sw_replay_mod()
{
    *(volatile int*)m_map_base = 0x01;
    return 0;
}
int CFpga_axi_lite::sw_encoder_mod()
{
    *(volatile int*)m_map_base = 0x00;
    return 0;
}